/**
 * 地址表
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  ADDRESS_TREE,
  ADDRESS_SAVEDATA
} from '../mutation-types'

const state = {
  addressTree: [],
  saveData: {}
}

const mutations = {
  [ADDRESS_TREE] (state, payload) {
    state.addressTree = payload
  },
  [ADDRESS_SAVEDATA] (state, data) {
    state.saveData = data
  }
}

const actions = {
  // 获取地址表
  async addressTree ({commit}, payload) {
    let treeRes = await api('serveraddress/address/ApiAddress/addressList', payload)
    if (SUCCESS_STATUS === treeRes.status) {
      commit(ADDRESS_TREE, treeRes.res)
    }
  },
  // 添加地址表
  async addressAdd ({commit}, payload) {
    let addRes = await api('serveraddress/address/ApiAddress/createAddress', payload)
    return promise(addRes)
  },
  // 编辑地址表
  async addressEdit ({commit}, payload) {
    let editRes = await api('serveraddress/address/ApiAddress/editAddress', payload)
    return promise(editRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
